﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    treeGrid: 'treeGrid'
}).use(['layer', 'table', 'laypage', 'laytpl', 'laydate', 'tree', 'treeGrid','jquery'], function () {
    var $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate,
        player = parent.layer || layui.layer,
        tree = layui.tree,
        treeGrid = layui.treeGrid; //很重要
    var authorityMenusListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                field: 'iSort',
                order: 'asc',
                rowData: undefined,//选中行信息
                listsUrl: "/Authority/Menus/LoadListInfo",
                addUrl: "/Authority/Menus/Add",
                modifyUrl: "/Authority/Menus/Modify",
                removeUrl: "/Authority/Menus/Remove",
                upMoveUrl: "/Authority/Menus/UpMove",
                downMoveUrl: "/Authority/Menus/DownMove",
                buttonsUrl: "/Authority/Menus/Buttons",
            };

            //this.Parameters.ac
            //    .pushQueue(function () {
            //        CommonLogic.loadButtonsInfo(function () {
            //            self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //        })
            //    })
            //    .pushQueue(function () {
            //        self.initPage();
            //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //    })
            //    .pushQueue(this.bindEvent)
            //    .exec();

            //页面初始化
            this.initPage();
            //时间初始化
            this.bindEvent();

        },
        //初始化列表
        initPage: function () {
            var treeTable = treeGrid.render({
                elem: '#menusList',
                url: self.Parameters.listsUrl,
                cellMinWidth: 100,
                height: "full-110",
                idField: 'gCode',//必須字段
                treeId: 'gCode',//树形id字段名称
                treeUpId: 'gParentCode',//树形父id字段名称
                treeShowName: 'sName',//以树形式显示的字段
                isOpenDefault: true,//节点默认是展开还是折叠【默认展开】
                cheDisabled: true,
                page: false,
                cols: [[
                    { type: 'radio' },//checkbox 
                    {
                        field: 'sName', title: '菜单名称', width: '180',
                        templet: function (d) {
                            return '<a><i class="iconfont ' + d.sIconFont + '" data-icon="' + d.sIconFont + '" ></i><cite>' + d.sName + '</cite></a>';
                        }
                    },
                    { field: 'sIconFont', title: '标签icon', width: '200' },
                    { field: 'iLevel', title: '级别', width: '80', align: "center" },
                    { field: 'sUrl', title: '链接地址', width: '230' },
                    { field: 'iSort', title: '排序', width: '80', align: "center" },
                    {
                        field: 'bIsEnabled', title: '是否启用', width: '90', align: "center",
                        templet: function (d) {
                            return d.bIsEnabled ? "启用" : "停用";
                        }
                    },
                    {
                        field: 'iType', title: '类型', width: '110', align: "center",
                        templet: function (d) {
                            return GrdEnum.getValue.menuTypeValue(d.iType);
                        }
                    },
                    { title: '操作', toolbar: '#gridTool', width: '220', align: 'left' }//fixed: 'right', 
                ]],
                onRadio: function (obj) {
                    self.Parameters.rowData = obj;
                    //单选事件
                    self.logicFunc.showHideBtn(obj);
                }
            });
        },
        //绑定事件
        bindEvent: function () {

            //监听(列表操作)
            treeGrid.on('tool(menusList)', function (obj) {
                var data = obj.data;
                self.Parameters.rowData = data;
                //新增子节点
                if (obj.event === 'funBtnAdd') {
                    self.logicFunc.addInfo();
                }
                //修改
                else if (obj.event === 'funBtnEdit') {
                    self.logicFunc.modifyInfo();
                }
                //按钮分配
                else if (obj.event === 'funBtnDistributionButton') {
                    self.logicFunc.selelctButtons();
                }
                //删除
                else if (obj.event === 'funBtnRemove') {
                    self.logicFunc.removeInfo();
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //新增根节点
            $("#funBtnAddRoot").on("click", function () {
                self.Parameters.rowData = null;
                self.logicFunc.addInfo();
            });

            //上移
            $("#funBtnTop").on("click", function () {
                self.logicFunc.move(1);
            });

            //下移
            $("#funBtnBottom").on("click", function () {
                self.logicFunc.move(2);
            });

            //对于单选框按钮点击行的选中功能：
            $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
                /*当单击表格行时,把单选按钮设为选中状态*/
                var tableDiv;
                var index = $(this).attr('data-index');
                var tableBox = $(this).parents('.layui-table-box');
                //存在固定列
                if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                    tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
                } else {
                    tableDiv = tableBox.find(".layui-table-body.layui-table-main");
                }
                var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.layui-table-cell div.layui-form-radio I");
                if (checkCell.length > 0) {
                    checkCell.click();
                }
            });
            //对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件  将此代码在页面初始化后执行一次即可以。
            $(document).on("click", "td div.layui-table-cell div.layui-form-radio", function (e) {
                e.stopPropagation();
            });

        },
        //逻辑方法
        logicFunc: {

            ///获取数据
            loadListInfo: function () {
                treeGrid.reload("menusList", {
                    page: {
                        curr: 1
                    },
                    where: {
                        parameters: GrdLayUI.zip($("#txtKeyWord").val()) 
                    }
                });
            },

            //增加信息
            addInfo: function () {
                var title = "增加根节点信息";
                var url = self.Parameters.addUrl;
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {
                    var data = {
                        gParentCode: self.Parameters.rowData.gCode,
                        iLevel: self.Parameters.rowData.iLevel + 10,
                        iSort: self.Parameters.rowData.iSort+1
                    };
                    title = "增加子节点信息";
                    url = self.Parameters.addUrl + '?data=' + GrdLayUI.zip(data);
                }
                var index = layui.layer.open({
                    title: title,
                    type: 2,
                    resize: false,
                    area: ['500px', '350px'],
                    content: url,
                    end: function () {
                        $(window).unbind("resize");
                        self.Parameters.rowData = null;
                    }
                });
            },

            //修改信息
            modifyInfo: function () {

                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {
                    var data = {
                        gCode: self.Parameters.rowData.gCode,
                        gParentCode: self.Parameters.rowData.gParentCode,
                        iLevel: self.Parameters.rowData.iLevel,
                        iSort: self.Parameters.rowData.iSort,
                        sName: self.Parameters.rowData.sName,
                        sIconFont: self.Parameters.rowData.sIconFont,
                        sUrl: self.Parameters.rowData.sUrl,
                        iType: self.Parameters.rowData.iType,
                        bIsEnabled: self.Parameters.rowData.bIsEnabled
                    };
                    var index = layui.layer.open({
                        title: "修改菜单信息",
                        type: 2,
                        resize: false,
                        area: ['500px', '350px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                        end: function () {
                            $(window).unbind("resize");
                            self.Parameters.rowData = null;
                        }
                    });
                }
            },

            //删除按钮信息
            removeInfo: function () {
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {

                    GrdLayUI.confirm("确定删除信息吗？", function () {
                        var arrayObj = new Array();//创建一个数组
                        arrayObj.push(self.Parameters.rowData.gCode);
                        var json = {
                            parameters: arrayObj
                        };
                        GrdLayUI.ajaxPost({
                            url: self.Parameters.removeUrl,
                            data: json,
                            success: function (result) {
                                if (result.Code == 1000) {
                                    GrdLayUI.msg(result.Msg, "成功", function () {
                                        self.logicFunc.loadListInfo();
                                    });
                                } else {
                                    GrdLayUI.msg(result.Msg, "警告");
                                }
                            }
                        });
                    });
                }
            },

            //显示或隐藏移动按钮
            showHideBtn: function (obj) {
                if (obj.bHaveElder) {
                    $("#funBtnTop").show();
                }
                else {
                    $("#funBtnTop").hide();
                }
                if (obj.bHaveYounger) {
                    $("#funBtnBottom").show();
                } else {
                    $("#funBtnBottom").hide();
                }
            },

            //移动操作 flag==1上移   flag==2下移
            move: function (flag) {
                if (self.Parameters.rowData!=null) {
                    var moveUrl = "";
                    if (flag == 1) {
                        moveUrl = self.Parameters.upMoveUrl;
                    } else {
                        moveUrl = self.Parameters.downMoveUrl;
                    }
                    var json = {
                        parameters: {
                            Flag: flag,
                            Code: self.Parameters.rowData.gCode
                        }
                    };
                    GrdLayUI.ajaxPost({
                        url: moveUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                self.logicFunc.loadListInfo();
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {
                    GrdLayUI.msg("请选择操作行！", "警告");
                }
            },

            //选择按钮
            selelctButtons: function () {
                if (self.Parameters.rowData != undefined
                    && self.Parameters.rowData != null) {
                    var index = layui.layer.open({
                        title: "选择按钮信息",
                        type: 2,
                        resize: false,
                        area: ['520px', '500px'],
                        content: self.Parameters.buttonsUrl + '?data=' + self.Parameters.rowData.gCode,
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                }
            }
        },
        //工具
        tools: {

        }
    };
    authorityMenusListLogic.init();
});
